package com.foreveross.service;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.common.Constants;
import com.foreveross.dao.HrFavorableDAO;
import com.foreveross.dao.OrderInfoDAO;
import com.foreveross.vo.HrFavorable;
import com.foreveross.vo.OrderInfo;

@Service("hrFavorableService")
@Transactional
public class HrFavorableService extends BaseService {

	@Resource
	public HrFavorableDAO hrFavorableDAO;
	@Resource
	public OrderInfoDAO orderInfoDAO;

	public long getFavorableListCount(String weixinID,String userId,String name,String houseId,String businessId) throws Exception{
		return hrFavorableDAO.getFavorableListCount(weixinID,userId,name,houseId,businessId);
	}

	public List<HrFavorable> getFavorableList(String weixinID,String userId,String name,String houseId,String businessId, int pageIndex,
			int pageSize) throws Exception{
		
		return hrFavorableDAO.getFavorableList(weixinID, userId,name,houseId,businessId,pageIndex,pageSize);
	}

	public HrFavorable getFavorableById(String id) throws Exception{
		
		return (HrFavorable) hrFavorableDAO.findById(HrFavorable.class, id);
	}
	
	/**
	 * 设置产品销售数量
	 * @param id 产品 FavorableID
	 * @throws Exception
	 */
	public void setCountForHrFavorable(String id)throws Exception{
		HrFavorable hrFavorable = getFavorableById(id);
		int paySuccessCount = 0;
		//int refundSuccessCount = 0;
		int offLineSuccessCount = 0;
		//int offLineFailCount = 0;
		
		//支付成功的
		List<OrderInfo> paySuccess = orderInfoDAO.findOrderInfoList(null, null, null, null, null, 
				hrFavorable.getId(), null, String.valueOf(Constants.PAY_STATUS_SUCCESS),
				null,null,null, 0, Constants.PAGE_MAX_SIZE);
		for(OrderInfo orderInfo : paySuccess){
			paySuccessCount += orderInfo.getCount();
		}
		
		/*//退款的
		List<OrderInfo> refundSuccess = orderInfoDAO.findOrderInfoList(null, null, null, null, null, 
				hrFavorable.getId(), null, String.valueOf(Constants.PAY_STATUS_REFUND_SUCCESS),
				null,null, 0, Constants.PAGE_MAX_SIZE);
		for(OrderInfo orderInfo : refundSuccess){
			refundSuccessCount += orderInfo.getCount();
		}*/
		
		//线下已提货的
		List<OrderInfo> offLineSuccess = orderInfoDAO.findOrderInfoList(null, null, null, null, null, 
				hrFavorable.getId(), null, String.valueOf(Constants.PAY_STATUS_OFFLINE_SUCCESS),
				null,null,null, 0, Constants.PAGE_MAX_SIZE);
		for(OrderInfo orderInfo : offLineSuccess){
			offLineSuccessCount += orderInfo.getCount();
		}
		
		/*//线下已失效的
		List<OrderInfo> offLineFail = orderInfoDAO.findOrderInfoList(null, null, null, null, null, 
				hrFavorable.getId(), null, String.valueOf(Constants.PAY_STATUS_OFFLINE_FAIL),
				null,null, 0, Constants.PAGE_MAX_SIZE);
		for(OrderInfo orderInfo : offLineFail){
			offLineFailCount += orderInfo.getCount();
		}*/
		
		int saleCount = paySuccessCount+offLineSuccessCount;
		hrFavorable.setSaleCount(saleCount);
		
		hrFavorableDAO.updateObject(hrFavorable);
	}



}
